<template>
  <n-switch v-bind="getSwitchProps" v-model:value="getSwitchValue" @update:value="changeValue" />
</template>

<script>
import { defineComponent, ref, unref, computed } from 'vue'
export default defineComponent({
  name: 'TableSwitch',
  props: {
    data: {
      type: [Object, String, Number, Boolean],
      required: true
    },
    rowKey: {
      type: String,
      default: ''
    }
  },
  emits: ['change'],
  setup(props, { emit }) {
    // const switchValue = ref()
    // const { data, rowKey } = unref(props)
    // switchValue.value = rowKey ? data[rowKey] : data
    const getSwitchValue = computed(() => {
      const { data, rowKey } = unref(props)
      const value = rowKey ? data[rowKey] : data
      return value
    })
    const getSwitchProps = computed(() => {
      return {
        ...unref(props)
      }
    })
    function changeValue(value) {
      const { data } = props
      const params = {
        data,
        value
      }
      emit('change', params)
    }
    return {
      // switchValue,
      getSwitchValue,
      getSwitchProps,
      changeValue
    }
  }
})
</script>
